#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

TIM_HandleTypeDef htim1;
typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    uint8_t i = 0;
    LCD_DCXLow();
    LCD_CSLow();
    for (i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par <<= 1;
    }
    LCD_CSHigh();
    LCD_DCXHigh();
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    uint8_t i = 0;
    LCD_DCXHigh();
    LCD_CSLow();
    for (i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if (par & 0x80)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par <<= 1;
    }
    LCD_CSHigh();
    LCD_DCXHigh();
}
//**********************************read function begin********************************************
void Write(uint8_t dc, uint8_t par)
{
    if (dc == Command)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else if (dc == Parameter)
    {
        wr_dat_4spi_8bcs(par);
    }
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(1);
    LCD_RSTLow();
    HAL_Delay(10);
    LCD_RSTHigh();
    HAL_Delay(120);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void initial(void)
{
    HW_Reset();

    Delay(120);

    Write(Command, 0x11);

    Delay(120);
    Write(Command, 0x35);
    Write(Parameter, 0x00);

    Write(Command, 0x36);
    Write(Parameter, 0x00);

    Write(Command, 0x3A);
    Write(Parameter, 0x06);

    Write(Command, 0xB2);
    Write(Parameter, 0x1F);
    Write(Parameter, 0x1F);
    Write(Parameter, 0x00);
    Write(Parameter, 0x33);
    Write(Parameter, 0x33);

    Write(Command, 0xB7);
    Write(Parameter, 0x00);

    Write(Command, 0xBB);
    Write(Parameter, 0x3F);

    Write(Command, 0xC0);
    Write(Parameter, 0x2C);

    Write(Command, 0xC2);
    Write(Parameter, 0x01);

    Write(Command, 0xC3);
    Write(Parameter, 0x0D);

    Write(Command, 0xC4);
    Write(Parameter, 0x20); // VDV, 0x20:0v

    Write(Command, 0xC6);
    Write(Parameter, 0x13); // 0x13:60Hz

    Write(Command, 0xD0);
    Write(Parameter, 0xA4);
    Write(Parameter, 0xA1);

    Write(Command, 0xD6);
    Write(Parameter, 0xA1); // sleep in后，gate输出为GND

    Write(Command, 0xE0);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x03);
    Write(Parameter, 0x09);
    Write(Parameter, 0x03);
    Write(Parameter, 0x03);
    Write(Parameter, 0x10);
    Write(Parameter, 0x2D);
    Write(Parameter, 0x43);
    Write(Parameter, 0x3F);
    Write(Parameter, 0x33);
    Write(Parameter, 0x0D);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x29);
    Write(Parameter, 0x32);

    Write(Command, 0xE1);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x10);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x0A);
    Write(Parameter, 0x2D);
    Write(Parameter, 0x33);
    Write(Parameter, 0x45);
    Write(Parameter, 0x3A);
    Write(Parameter, 0x14);
    Write(Parameter, 0x19);
    Write(Parameter, 0x31);
    Write(Parameter, 0x37);

    Write(Command, 0xE4);
    Write(Parameter, 0x1D); // 使用240根gate  (N+1)*8
    Write(Parameter, 0x00); // 设定gate起点位置
    Write(Parameter, 0x00); // 当gate没有用完时，bit4(TMG)设为0

    Write(Command, 0x21);

    Write(Command, 0x29);

    Write(Command, 0x2A); // Column Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF); // 239

    Write(Command, 0x2B); // Row Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF); // 239

    Write(Command, 0x2C);
}

void keywait(uint32_t time)
{
    uint32_t i = 0;
    int pausing = 1;
    for (i = 0; i < time; i++)
    {
        if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
        {
            HAL_Delay(20);
            if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
            {
                while (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
                {
                    HAL_Delay(100);
                    printf("wait release\n\r");
                }
                printf("get key\n\r");
                while (pausing != 0)
                {
                    printf("pausing\n\r");
                    HAL_Delay(20);
                    if (HAL_GPIO_ReadPin(PAUSE_PA5_GPIO_Port, PAUSE_PA5_Pin) == GPIO_PIN_RESET)
                    {
                        printf("get 2nd key\n\r");
                        i = time - 2;
                        pausing = 0;
                        break;
                    }
                }
            }
        }
        HAL_Delay(1);
    }
}

void ctrl_LED(uint8_t LED_R, uint8_t LED_G, uint8_t LED_B)
{
    if (LED_R == 1)
    {
        HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
    }

    if (LED_G == 1)
    {
        HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET);
    }

    if (LED_B == 1)
    {
        HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
    }
}

void wait_button(GPIO_TypeDef *GPIOx, uint16_t pin)
{
    uint8_t waiting = 1;
    printf("wait button\n\r");
    while (waiting)
    {
        if (HAL_GPIO_ReadPin(GPIOx, pin) == GPIO_PIN_RESET)
        {
            printf("get key press\n\r");
            HAL_Delay(20);
            if (HAL_GPIO_ReadPin(GPIOx, pin) == GPIO_PIN_RESET)
            {
                while (HAL_GPIO_ReadPin(GPIOx, pin) == GPIO_PIN_RESET)
                {
                    HAL_Delay(1);
                }
                waiting = 1;
                break;
            }
        }
        else
        {
            waiting = 1;
        }
    }
}

unsigned char read_te_io(void)
{
    uint8_t value = 0;
    if (HAL_GPIO_ReadPin(TEST_TE_GPIO_Port, TEST_TE_Pin) == GPIO_PIN_SET)
    {
        value = 1;
    }
    else if (HAL_GPIO_ReadPin(TEST_TE_GPIO_Port, TEST_TE_Pin) == GPIO_PIN_RESET)
    {
        value = 0;
    }
    return value;
}

void test_te(void)
{
    uint32_t time = 100;
    for (uint32_t i = 0; i < time; i++)
    {
        if (read_te_io() == 1)
        {
            printf("get TE\n\r");
            break;
        }
        if (i == time - 1)
        {
            printf("not get TE\n\r");
            bmpDrawFile("3.bmp");
            while (1)
            {
                HAL_Delay(100);
            }
        }
        HAL_Delay(1);
    }

    uint8_t count = 0;
    while (count < 3)
    {
        HAL_Delay(1);
        while (read_te_io() == 0)
        {
            HAL_Delay(1);
            if (read_te_io() == 1)
            {
                printf("get TE == H\n\r");
                count = count + 1;
                break;
            }
        }
    }
    bmpDrawFile("4.bmp");
    HAL_Delay(1000);
}

void disp_main(void)
{
    sdcard_init();
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 启动PWM
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000);
    test_te();

    bmpDrawFile("11.bmp");
    wait_button(BUTTON_A_GPIO_Port, BUTTON_A_Pin);
    bmpDrawFile("12.bmp");
    wait_button(BUTTON_B_GPIO_Port, BUTTON_B_Pin);
    bmpDrawFile("13.bmp");
    wait_button(BUTTON_C_GPIO_Port, BUTTON_C_Pin);

    while (1)
    {
        bmpDrawFile("1.bmp");
        keywait(Interval);
        ctrl_LED(1, 1, 1);
        bmpDrawFile("2.bmp");
        keywait(Interval);
        __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 500);
        keywait(Interval);
        __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000);
        ctrl_LED(1, 0, 0);
        bmpDrawFile("3.bmp");
        keywait(Interval);
        ctrl_LED(0, 1, 0);
        bmpDrawFile("4.bmp");
        keywait(Interval);
        ctrl_LED(0, 0, 1);
        bmpDrawFile("5.bmp");
        keywait(Interval);
        ctrl_LED(0, 0, 0);
        bmpDrawFile("6.bmp");
        keywait(Interval);
        bmpDrawFile("7.bmp");
        keywait(Interval);
        bmpDrawFile("8.bmp");
        keywait(Interval);
        bmpDrawFile("9.bmp");
        keywait(Interval);
        bmpDrawFile("10.bmp");
        keywait(Interval);
    }
}